﻿module Sustainalytics.News.Admin {

    export class NewsCategoryService implements INewsCategoryService {

        private items: NewsCategory[];
        private $q: ng.IQService;

        constructor($q: ng.IQService) {

            this.$q = $q;

            this.items =
            [
                { id: 1, name: 'Corporate' },
                { id: 2, name: 'Corporate Responsibility' },
                { id: 3, name: 'Esg Research' },
                { id: 4, name: 'Responsible Investment' },
                { id: 5, name: 'RI Alerts' }
            ];
        }

        // get news category by ID
        public getNewsCategory(id: number): ng.IPromise<NewsCategory> {
            var def: ng.IDeferred<NewsCategory> = this.$q.defer();

            var item: NewsCategory = null;
            $(this.items).each(function () {
                if (this.id == id) {
                    item = this;
                    return false;
                }
            });

            def.resolve(item);
            return def.promise;
        }

        // get all news categories
        public getNewsCategories(): ng.IPromise<NewsCategory[]> {
            var def: ng.IDeferred<NewsCategory[]> = this.$q.defer();
            def.resolve(this.items);
            return def.promise;
        }
    }

    // register service with News Angular module
    var mod = angular.module(newsAdminModuleName);
    
    mod.service('NewsCategoryService',
        ['$q', NewsCategoryService]);
}